home *** CD-ROM | disk | FTP | other *** search
- unit TraceableClass;
-
- interface
-
- type
- {: This class just prints a trace of the different calls to
- methods intervening in object construction and destruction that
- can be modified by the end user. }
- TTraceableBaseClass = class
- public
- procedure AfterConstruction; override;
- procedure BeforeDestruction; override;
- constructor Create;
- destructor Destroy; override;
- end;
-
- {: Derived class provided to show how base and derived classes
- collaborate in construction and destruction. }
- TTraceableClass = class( TTraceableBaseClass )
- public
- procedure FreeInstance; override;
- class function NewInstance: TObject; override;
-
- procedure AfterConstruction; override;
- procedure BeforeDestruction; override;
- constructor Create;
- destructor Destroy; override;
- end;
-
- TWhenToRaise = (rpNone, rpNewInstance,
- rpAfterConstruction, rpBaseAfterConstruction, rpCreate, rpBaseCreate );
-
- {: Shows a trace of object construction and destruction,
- with the possibility of an exception being raised in different
- steps of the object construction.
-
- This provides an easy way of understing how the whole construction
- and destruction process works normally, as well as in the presence of
- exceptional conditions in each step of the construction process.
- }
- procedure Trace( when : TWhenToRaise );
-
- implementation
-
- uses
- SysUtils,
- WMain;
-
- var
- WhenToRaise : TWhenToRaise;
-
- procedure Trace( when : TWhenToRaise );
- var
- obj : TTraceableClass;
- begin
- WhenToRaise := when;
- try
- obj := TTraceableClass.Create;
- WriteLn( ' Construction has finished successfully!' );
- obj.Free;
- except
- // Just "eat" the exception, we report it elsewhere
- end;
- WriteLn;
- end;
-
- {: Raises an exception at the specified moment: in NewInstance, in
- the base class Create, etc.
- }
- procedure CheckRaise( when : TWhenToRaise );
- const
- Place : array[rpNewInstance..rpBaseCreate] of String =
- ( 'NewInstance',
- 'AfterConstruction', 'BASE.AfterConstruction',
- 'Create', 'BASE.Create' );
- var
- msg : string;
- begin
- if WhenToRaise = when then begin
- msg := ' ^ Exception raised in ' + Place[when];
- WriteLn( msg );
- raise Exception.Create( msg );
- end;
- end;
-
- { TTraceableBaseClass }
-
- procedure TTraceableBaseClass.AfterConstruction;
- begin
- inherited;
- CheckRaise( rpBaseAfterConstruction );
- WriteLn( 'BASE.AfterConstruction' );
- end;
-
- procedure TTraceableBaseClass.BeforeDestruction;
- begin
- inherited;
- WriteLn( 'BASE.BeforeDestruction' );
- end;
-
- constructor TTraceableBaseClass.Create;
- begin
- CheckRaise( rpBaseCreate );
- inherited;
- WriteLn( 'BASE.Create' );
- end;
-
- destructor TTraceableBaseClass.Destroy;
- begin
- WriteLn( 'BASE.Destroy' );
- inherited;
- end;
-
- { TTraceableClass }
-
- procedure TTraceableClass.AfterConstruction;
- begin
- inherited;
- CheckRaise( rpAfterConstruction );
- WriteLn( 'AfterConstruction' );
- end;
-
- procedure TTraceableClass.BeforeDestruction;
- begin
- WriteLn( 'BeforeDestruction' );
- inherited;
- end;
-
- constructor TTraceableClass.Create;
- begin
- inherited;
- CheckRaise( rpCreate );
- WriteLn( 'Create' );
- end;
-
- destructor TTraceableClass.Destroy;
- begin
- WriteLn( 'Destroy' );
- inherited;
- end;
-
- procedure TTraceableClass.FreeInstance;
- begin
- inherited;
- WriteLn( 'FreeInstance' );
- end;
-
- class function TTraceableClass.NewInstance: TObject;
- begin
- CheckRaise( rpNewInstance );
- Result := inherited NewInstance;
- WriteLn( 'NewInstance' );
- end;
-
- end.
-